<!-- Sofija Puric 6/08 -->
<?php

include("../../inc_mailer/class.phpmailer.php");
include("../../inc_mailer/class.smtp.php");

require '../../pages/db.php';
$con=dbConnect("sib");

$rid = $_GET["rid"];

function send_mail($to, $name, $subject, $body) {
    $mail             = new PHPMailer();

$mail->IsSMTP();
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port

$mail->Username   = "office.sib011@gmail.com";  // GMAIL username
$mail->Password   = "sibsib011";            // GMAIL password

$mail->From       = "office.sib011@gmail.com";
$mail->FromName   = "StayInBelgrade";
$mail->Subject    = $subject;
//$mail->AltBody    = "This is the body when user views in plain text format"; //Text Body
$mail->WordWrap   = 50; // set word wrap

$mail->Body=$body;

$mail->AddAddress($to, $name);

$mail->IsHTML(true); // send as HTML

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message has been sent";
}
}

function status_reservation($res_status) {
      $con=dbConnect("sib");
      $rid = $_GET["rid"];
      $sql_res = "SELECT * FROM reservation WHERE IDRes = '$rid'";
      $result_res = mysql_query($sql_res, $con);
      if (!$result_res)
            die('Error: ' . mysql_error());
      $row_res = mysql_fetch_array($result_res);
      $hid = $row_res["IDHostel"];
      $uid = $row_res["IDUser"];
      $sql_hos = "SELECT * FROM hostel WHERE IDHostel = '$hid'";
      $result_hos = mysql_query($sql_hos, $con);
      if (!$result_hos)
            die('Error: ' . mysql_error());
      $row_hos = mysql_fetch_array($result_hos);
      $sql_user = "SELECT * FROM user WHERE IDUser = '$uid'";
      $result_user = mysql_query($sql_user, $con);
      if (!$result_user)
            die('Error: ' . mysql_error());
      $row_user = mysql_fetch_array($result_user);

      // mail koji automatski stize korisniku nakon sto njegova rezervacija bude prihvacena ili odbijena
      $to = $row_user["Email"]; $n_s = $row_user["FirstName"].' '.$row_user["LastName"];
      $subject = "Information about reservation";
      $message = "<html><head></head><body>Dear " . $row_user["FirstName"] . " " . $row_user["LastName"] . ",
            your reservations for the hostel ".$row_hos["Name"]." for the period from ".$row_res["DateFrom"]."
            to ".$row_res["DateTo"]." is ".$res_status.". Thank you. All the best!
            StayInBelgrade</body></html>";
      send_mail($to, $n_s, $subject, $message);
    
}

if (isset($_POST["status"]) && !empty($_POST["status"])){

$opt=$_POST["status"];

switch ($opt) {
    case 1: {
        $sql_update="UPDATE reservation SET Status = 'Accepted' WHERE IDRes = '$rid'";
        status_reservation("Accepted");
        break;
    }
    case 2: {
        $sql_update="UPDATE reservation SET Status = 'Denied' WHERE IDRes = '$rid'";
        status_reservation("Denied");
        break;
    }
    default: header('Location: ../reservations-admin.php');
}

$result = mysql_query($sql_update, $con);
if (!$result)
    die('Error: ' . mysql_error());
else
    header('Location: ../reservations-admin.php');

}
else
    header('Location: ../reservations-admin.php');
mysql_close($con);



?>
